package com.fenghongzhang.test;

import android.content.Context;
import android.graphics.Canvas;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;

public class MyLieaner extends ViewGroup {
    private static final String TAG = "MyLieaner";
    private boolean isH;
    public MyLieaner(Context context) {
        super(context);
    }

    public MyLieaner(Context context, AttributeSet attrs) {
        super(context, attrs);
    }


    //onMesure() ——计算childView的测量值以及模式，以及设置自己的宽和高。
    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);


//        final int childCount = getChildCount();
//        for (int i = 0; i < childCount; i++) {
//            View childView = getChildAt(i);
//            int width = childView.getMeasuredHeight();
//            Log.i(TAG, "onMeasure: +"+width);
//            int width1 = childView.getWidth();
//            Log.i(TAG, "onMeasure: -"+width1);
//            int height = childView.getMeasuredWidth();
//            MarginLayoutParams layoutParams = (MarginLayoutParams) childView.getLayoutParams();
//            Log.i(TAG, "onMeasure: 子宽"+layoutParams.width);
//            Log.i(TAG, "onMeasure: 子高"+layoutParams.height);

//            childView.measure(layoutParams.width, layoutParams.height);
            //先测量子控件，再测量自己；必须,否则子空间没有数据
//            measureChildren(widthMeasureSpec,heightMeasureSpec);
//        }

//        int childCount = getChildCount();
//        if (childCount==0){
//            setMeasuredDimension(widthMeasureSpec,heightMeasureSpec);
//        }

        int width;
        int mode = MeasureSpec.getMode(widthMeasureSpec);
        int size = MeasureSpec.getSize(widthMeasureSpec);

        if(mode == MeasureSpec.EXACTLY){
            width = size;
        }else{
            width = 0;
        }

        int height;
        int heightMode = MeasureSpec.getMode(heightMeasureSpec);
        int heightSize = MeasureSpec.getSize(heightMeasureSpec);
        if(heightMode == MeasureSpec.EXACTLY){
            height = heightSize;
        }else{
            height = 0;
        }
        Log.i(TAG, "onMeasure: "+width +"---"+height);
        setMeasuredDimension(width,height);

    }

    //在onMeasure()后执行，只有大小发生了变化才会执行
    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
    }

    //通过getChildCount()获取子view数量，
    // getChildAt获取所有子View，
    // 分别调用layout(int l, int t, int r, int b)确定每个子View的摆放位置。
    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {
//        int left , top ,right ,bottom;

        int width = 0;

        int left = 0;
        int childCount = getChildCount();
        for (int i = 0; i < childCount; i++) {
            View childAt = getChildAt(i);
            MarginLayoutParams layoutParams = (MarginLayoutParams) childAt.getLayoutParams();
//            int measuredWidth = childAt.getMeasuredWidth();
            int measuredWidth = layoutParams.width;
            Log.i(TAG, "onLayout:子宽 "+measuredWidth);
            //所有子空间的宽度
            width += measuredWidth;
            int childHeight = layoutParams.height;
//            int measuredHeight = childAt.getMeasuredHeight();
//            Log.i(TAG, "onLayout:子高 "+measuredHeight);
            Log.i(TAG, "onLayout:子高 "+childHeight);
//            Log.i(TAG, "onLayout: 父容器宽度"+getWidth());
            int height = getHeight();
            Log.i(TAG, "onLayout: 父高"+height);
            if(width > getWidth()){
                Log.i(TAG, "onLayout: 超");
//                childAt.layout(0,20,measuredWidth,childHeight);
                childAt.layout(0,childHeight,measuredWidth,childHeight*2);
            }else{
                Log.i(TAG, "onLayout: 不超");
//                子view的上下左右的值，均是相对于父控件的
                childAt.layout(left, 0, left + measuredWidth, childHeight);
                left += measuredWidth;
//                childAt.layout(0,200,measuredWidth,childHeight+200);
            }
        }
    }

    //默认不会触发，需要手动触发。
    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

    }

    @Override
    public LayoutParams generateLayoutParams(AttributeSet attrs) {
        return new LinearLayout.LayoutParams(getContext(),attrs);
    }
}
